package export.huodong.mysterious
{
	import com.dusk.util.TextUtil;
	import com.game.data.constant.ConfigConst;
	import com.game.event.GameEvent;
	import com.game.manager.EventManager;
	import com.game.manager.NotificationManager;
	
	import config.Config;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class MysteriousShop extends Sprite
	{
		
		public function MysteriousShop()
		{
			super();
			TextUtil.redrawTxtOld(this, ["txttimes", "txtlh"]);
			this.gc = Config.getIns();
			txttimes.embedFonts = true;
			txttimes.defaultTextFormat.font = ConfigConst.GAME_CONFIG_FONT;
			txtlh.embedFonts = true;
			txtlh.defaultTextFormat.font = ConfigConst.GAME_CONFIG_FONT;
			this.addEventListener(Event.ADDED_TO_STAGE, this.added, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.removed, false, 0, true);
		}
		
		public var x_btn:SimpleButton;
		public var open_btn:SimpleButton;
		public var addtime_btn:SimpleButton;
		public var sp1:SingleShop;
		public var sp2:SingleShop;
		public var sp3:SingleShop;
		public var sp4:SingleShop;
		public var sp5:SingleShop;
		public var txttimes:TextField;
		public var txtlh:TextField;
		public var lastBuy:int;
		private var gc:Config;
		private var biggest:int;
		private var hasbuy:Boolean = false;
		private var openall:Boolean = false;
		
		public function setBiggest(param1:int):void
		{
			this.biggest = param1;
		}
		
		public function getBiggest():int
		{
			return this.biggest;
		}
		
		public function getTotalCoin(param1:int = 3):int
		{
			return gc.playerData.getStuffNumByName("xhb")
		}
		
		private function setText():void
		{
			this.txtlh.text = this.getTotalCoin() + "";
			for (var i:int = 1; i <= 5; i++)
			{
				this["sp" + i].setTheShopNum(i)
			}
		}
		
		private function added(param1:Event):void
		{
			this.x_btn.addEventListener(MouseEvent.CLICK, this.xClick);
			this.open_btn.addEventListener(MouseEvent.CLICK, this.openAllClick);
			this.addtime_btn.addEventListener(MouseEvent.CLICK, this.addTimesClick);
			EventManager.getIns().registerListen("MysteriousShopBuySuccess", this.buyOver);
			this.setBiggest(1);
			this.txttimes.text = 20 + "";
			this.setText();
		}
		
		private function removed(param1:Event):void
		{
			this.x_btn.removeEventListener(MouseEvent.CLICK, this.xClick);
			this.open_btn.removeEventListener(MouseEvent.CLICK, this.openAllClick);
			this.addtime_btn.removeEventListener(MouseEvent.CLICK, this.addTimesClick);
			EventManager.getIns().removeListen("MysteriousShopBuySuccess", this.buyOver);
			this.openall == "false";
		}
		
		private function buyOver(param1:GameEvent):void
		{
			var m:* = undefined;
			var _loc3_:* = undefined;
			this.hasbuy = true;
			_loc3_ = this.lastBuy;
			if (this.openall)
			{
				this.openall = false;
				for (m in [0, 0, 0, 0, 0])
				{
					this["sp" + ++m].addMask();
				}
				this.lastBuy = 1;
				_loc3_ = 1;
			}
			if (_loc3_ == 1)
			{
				if (this.getBiggest() == 1)
				{
					if (Math.random() > 0.5)
					{
						this.sp2.removedMask();
						this.setBiggest(2);
					}
				}
			}
			else if (_loc3_ == 2)
			{
				if (Math.random() > 0.5)
				{
					this.sp3.removedMask();
					this.setBiggest(3);
				}
				else
				{
					this.setBiggest(1);
				}
				this.sp2.addMask();
			}
			else if (_loc3_ == 3)
			{
				if (Math.random() > 0.5)
				{
					this.sp4.removedMask();
					this.setBiggest(4);
				}
				else
				{
					this.setBiggest(1);
				}
				this.sp3.addMask();
			}
			else if (_loc3_ == 4)
			{
				if (Math.random() > 0.5)
				{
					this.sp5.removedMask();
					this.setBiggest(5);
				}
				else
				{
					this.setBiggest(1);
				}
				this.sp4.addMask();
			}
			else if (_loc3_ == 5)
			{
				this.sp5.addMask();
				this.sp4.addMask();
				this.sp3.addMask();
				this.sp2.addMask();
				this.setBiggest(1);
			}
			this.setText();
		}
		
		private function xClick(param1:MouseEvent):void
		{
			if (this.parent)
			{
				this.parent.removeChild(this);
			}
		}
		
		private function openAllClick(param1:MouseEvent):void
		{
			var m:* = undefined;
			if (Math.random() <= 0.95)
			{
				NotificationManager.alert("这里什么都没有!");
			}
			else
			{
				for (m in [0, 0, 0, 0, 0])
				{
					this["sp" + ++m].removedMask();
				}
				this.openall = true;
				this.gc.playerData.removeEquipFormBag("xhb", 2, 15);
				this.setText();
				NotificationManager.alert("这都给你发现了？！");
				this.gc.memory.saveGame(gc.saveId);
			}
		}
		
		private function addTimesClick(param1:MouseEvent):void
		{
			if (this.txttimes.text == "0")
			{
				NotificationManager.alert("命运如此，从头再来");
			}
			else
			{
				NotificationManager.alert("不用增加次数哦");
			}
		}
	}
}
